package cn.hdu.charge.servlet;

import cn.hdu.charge.domain.Record;
import cn.hdu.charge.domain.User;
import cn.hdu.charge.service.RecordService;
import cn.hdu.utils.commons.CommonUtils;
import cn.hdu.utils.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/RecordServlet")
public class RecordServlet extends BaseServlet {

    private RecordService recordService = new RecordService();

    public void getRecord(HttpServletRequest req, HttpServletResponse resp)
                                throws ServletException, IOException {
        User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
        outResp(resp, recordService.getRecord(formUser));
    }

    public void startCharge(HttpServletRequest req, HttpServletResponse resp)
                                throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Record formRecord = CommonUtils.toBean(map, Record.class);
        User user = CommonUtils.toBean(map, User.class);
        formRecord.setUser(user);
        outResp(resp,recordService.startCharge(formRecord));
    }

    public void endCharge(HttpServletRequest req, HttpServletResponse resp)
                                throws ServletException, IOException {
        Record formRecord = CommonUtils.toBean(req.getParameterMap(), Record.class);
        outResp(resp,recordService.endCharge(formRecord));
    }
}
